/********************************************************************************
 ***                                                                          *** 
 ********************************************************************************
 *                                                                              * 
 *           Prject Name : MEMORIA                                              * 
 *                                                                              * 
 *             File Name : card.cpp                                             * 
 *                                                                              * 
 *            Programmer : LBW                                                  * 
 *                                                                              * 
 *            Start Date : 2020/12/26                                           * 
 *                                                                              * 
 *           Last Update : 2020/12/26  [LBW]                                    * 
 *                                                                              * 
 *------------------------------------------------------------------------------*
 * Functions:                                                                   * 
 *   Card::Card -- 构造用于测试的卡片                                           * 
 *   Card::paintEvent -- Qt中改变部件样式需要定义的事件                         * 
 *==============================================================================*/

#include <QtWidgets>
#include <QVector>
#include <QStringList>
#include <QtGlobal>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>

#include "parameter.h"
#include "card.h"
#include "line.h"
#include "wordline.h"

/**********************************************************************************
 * Card::Card -- 构造用于测试的卡片                                               *
 *                                                                                *
 *   使用strSentence,strAnswer,strWordPos构建Line                                 *
 *   使用strAnswer,strWordPos解析出的wordPos[]、wordLength[],和strInterference    *
 *   构建WordLine                                                                 *
 *                                                                                *
 * INPUT: cardId -- 卡片在整个卡片集中所处的位置                                  *
 *        strHeading -- 小标题                                                    *
 *        strSentence -- 翻译好的的母语句子                                       *
 *        strAnswer -- 需要记忆的外语句子                                         *
 *        strWordPos -- 需要记忆的单词所在的位置和长度信息                        *
 *        strInterference -- 干扰词                                               *
 *        parent -- Card的父对象                                                  *
 *                                                                                *
 * OUTPUT: none                                                                   *
 *                                                                                *
 * WARNINGS: none                                                                 *
 *                                                                                *
 * HISTORY:                                                                       *
 *   2020/12/26 LBW : Created.                                                    *
 *================================================================================*/
Card::Card(int cardId,QString strHeading,QString strSentence,QString strAnswer,QString strWordPos,QString strInterference,QWidget *parent):QWidget(parent)
{	
	Line *line = new Line(strSentence,strAnswer,strWordPos,this);


	/*
	** 其实可以在这里解析strWordPos,用wordPos[]、wordLength[]分别构建Line和WordLine比较好
	*/
	id = cardId;
	numOfWords = line->getNumOfWords();
	wordPos.resize(numOfWords);
	wordLength.resize(numOfWords);

	for(int i=0;i<numOfWords;i++){
		wordPos[i] = line->getWordPos(i);
		wordLength[i] = line->getWordLength(i);
	}
	
	WordLine *wordLine = new WordLine(strAnswer,wordPos,wordLength,strInterference,true,this);

	headingLabel = new QLabel(strHeading);
	headingLabel->setFixedHeight(headingLabel->sizeHint().height());	

	QScrollArea *scrollArea = new QScrollArea;
	scrollArea->setFixedHeight(LINEFIXEDHEIGHT);
	scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	scrollArea->setWidgetResizable(false);
	scrollArea->setWidget(line);

	QScrollArea *wordScrollArea = new QScrollArea;
	wordScrollArea->setFixedHeight(WORDFIXEDHEIGHT);
	wordScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	wordScrollArea->setWidgetResizable(true);
	wordScrollArea->setWidget(wordLine);


	//设置Layout
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
	mainLayout->setAlignment(Qt::AlignTop);
	mainLayout->addWidget(headingLabel);
	mainLayout->addWidget(scrollArea);
	mainLayout->addWidget(wordScrollArea);

	setVisible(false);
	setStyleSheet(QString(".Card{background:transparent;border:1px solid gray}"));
	setLayout(mainLayout);
	setFixedHeight(CARDFIXEDHEIGHT);
}

/**********************************************************************************
 * Card::paintEvent -- Qt中改变部件样式需要定义的事件                             *
 *                                                                                *
 * INPUT: QPainEvent *                                                            *
 *                                                                                *
 * OUTPUT: none                                                                   *
 *                                                                                *
 * WARNINGS: none                                                                 *
 *                                                                                *
 * HISTORY:                                                                       *
 *   2020/12/26 LBW : Created.                                                    *
 *================================================================================*/
void Card::paintEvent(QPaintEvent *)
{
	QStyleOption opt;
	opt.init(this);
	QPainter p(this);
	style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);
}





















